package com.tanhua.server.service;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.tanhua.common.mapper.QuestionMapper;
import com.tanhua.common.pojo.Question;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author Don
 */
@Service
public class QuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    public Question queryQuestion(Long userId) {
        LambdaQueryWrapper<Question> query = new LambdaQueryWrapper<>();
        query.eq(Question::getUserId, userId);
        return questionMapper.selectOne(query);
    }
    /**
     *  保存问题
     *
     * @param userId
     * @param content
     */
    public void saveQuestion(Long userId,String content) {
        // 查询问题
        Question queryQuestion = this.queryQuestion(userId);
        if(null != queryQuestion){
            // 如果问题不为空，则对问题进行更新
            queryQuestion.setTxt(content);
            this.questionMapper.updateById(queryQuestion);
        }

        // 如果为空，则需添加
        Question question = new Question();
        question.setId(userId);
        question.setTxt(content);
        this.questionMapper.insert(question);
    }
}
